// packageD/auction/auction/auction_index/auction_index.js
var app = getApp();
import utils from '../../../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    activeName: '0',
    tabArr: ['全部', '竞拍中', '即将开始', '已结束'],
    isLoadMore: true,
    page: 1,
    last_page: 0,
    current_page: 1,

    //新样式的数据
    styleType: 2,
    auctionColor: "#f5f5f5",
    auctionImg: "",
    beginStatus: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getdata();
    this.getNew();
  },
  onChange(e) {
    console.log(e)
    let ind = e.detail.index;
    this.setData({
      activeName: ind
    })
    this.initData();
    if (ind != 0) {
      this.newListData();
    } else {
      this.getNew();
    }
  },
  initData() {
    this.setData({
      loading: false,
      isLoadMore: false,
      page: 1,
      total_page: 0,
      datas: [],
      lsitData: [],
    })
  },
  tolink(e) {
    let urls = e.currentTarget.dataset.item.small_url;
    wx.navigateTo({
      url: urls,
    });
  },
  slideNavTo(e) {
    console.log(e);
    let urls = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: urls,
    });
  },
  imgH(e){
    var winWid = wx.getSystemInfoSync().windowWidth;//获取当前屏幕的宽度
    var imgh=e.detail.height;//图片高度
    var imgw=e.detail.width;
    var swiperH=winWid*imgh/imgw + "px"　//等比设置swiper的高度。  即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度    ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
    this.setData({
        Hei:swiperH//设置高度
    })
  },
  goMy() {
    wx.navigateTo({
      url: '/packageD/auction/auction-mine/my_index/my_index',
    });
  },
  gosearch() {
    wx.navigateTo({
      url: '/packageD/auction/auction/auction_search/auction_search',
    });
  },
  toMore(e) {
    let is_id = e.currentTarget.dataset.is_recommend;
    wx.navigateTo({
      url: '/packageD/auction/auction/auction_more/auction_more?is_recommend=' + is_id,
    });
  },
  toMoreNew(e) {
    let is_id = e.currentTarget.dataset.is_new;
    wx.navigateTo({
      url: '/packageD/auction/auction/auction_more/auction_more?is_new=' + is_id,
    });
  },
  toDetail(e) {
    let id = e.currentTarget.dataset.id.goods_id;
    let member_id = e.currentTarget.dataset.id.member_id ? e.currentTarget.dataset.id.member_id : e.currentTarget.dataset.id.auctioneer_id;
    wx.navigateTo({
      url: '/packageD/auction/auction/auction_good/auction_good?id=' + id + '&member_id=' + member_id,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getNewMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getNew() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    let json = {
      keyword: '',
      status: 1,
      auc_status: '',
      is_new: 1,
      is_recommend: '',
      auctioneer_id: '',
      art: 'getSearch',
      page: this.data.page,
    };
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          let data = res.data;
          let auctionStyle = data?.auction_style;
          that.setData({
            datas: res.data.getSearch.data,
            current_page: res.data.getSearch.current_page,
            last_page: res.data.getSearch.last_page,
            styleType:  auctionStyle.auction_style,
            auctionColor: auctionStyle.auction_style_color,
            auctionImg: auctionStyle.auction_style_image
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getdata() {
    var that = this;
    let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
    let json = {
      art: 'index'
    };
    app._postNetWork({
      url: urlStr,
      data: json,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          let data = res.data;
          for (let i = 0; i < res.data.index.recommend.length; i++) {
            res.data.index.recommend[i].end_shooting = utils.formatTimeTwo(res.data.index.recommend[i].end_shooting, 'Y-M-D h:m:s');
          }
          that.setData({
            rotation: res.data.index.rotation,
            recommend: res.data.index.recommend,
            styleType: data?.auction_style.auction_style
          });
          console.log(that.data.styleType);
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getNewMore() {
    var that = this;
    if (this.data.current_page >= this.data.last_page) {
      // wx.showToast({
      //   title: '没有更多',
      //   icon: 'none',
      //   duration: 1000
      // });
      return;
    } else {
      let loadpage = that.data.page + 1;
      that.setData({
        page: loadpage
      });
      if (this.data.activeName == 0) {
        let urlStr = app.getNetAddresss('plugin.auction.api.index.getAuctionData');
        let json = {
          keyword: '',
          status: 1,
          auc_status: '',
          is_new: 1,
          is_recommend: '',
          auctioneer_id: '',
          art: 'getSearch',
          page: this.data.page,
        };
        app._postNetWork({
          url: urlStr,
          data: json,
          success: function (resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              console.log(res);
              that.setData({
                datas: that.data.datas.concat(res.data.getSearch.data),
                current_page: res.data.getSearch.current_page,
                last_page: res.data.getSearch.last_page
              });
            } else {
              wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 1000
              });
            }
          },
          fail: function (res) {
            console.log(res);
          }
        });
      } else {
        let urlStr = app.getNetAddresss('plugin.auction.api.index.index-page');
        let json = {
          auction_type: this.data.activeName,
          page:this.data.page
        };
        app._postNetWork({
          url: urlStr,
          data: json,
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              this.setData({
                lsitData: this.data.lsitData(res.data.list.data),
                current_page: res.dta.list.current_page,
                last_page: res.data.list.last_page,
              })
            } else {
              wx.showToast({
                title: res.msg,
                icon: "none",
                duration: 1000
              });
            }
          },
          fail: function (res) {
            console.log(res);
          }
        });
      }
    }
  },
  newListData() {
    let urlStr = app.getNetAddresss('plugin.auction.api.index.index-page');
    let json = {
      auction_type: this.data.activeName
    };
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          this.setData({
            lsitData: res.data.list.data,
            current_page: res.data.list.current_page,
            last_page: res.data.list.last_page,
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
});